﻿using Kent.Boogaart.Converters;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;

// http://www.robzelt.com/blog/2008/10/19/Silverlight+2+Timeline+Panel.aspx

namespace RenameTracker
{
    #region TimelinePanel

    public class TimelinePanel : Panel
    {
        #region VerticalOffset

        /// <summary>
        /// VerticalOffset Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty VerticalOffsetProperty =
            DependencyProperty.RegisterAttached("VerticalOffset", typeof(double)
            , typeof(TimelinePanel), null);

        /// <summary>
        /// Gets the VerticalOffset property.  This dependency property 
        /// indicates ....
        /// </summary>
        public static double GetVerticalOffset(DependencyObject d)
        {
            return (double)d.GetValue(VerticalOffsetProperty);
        }

        /// <summary>
        /// Sets the VerticalOffset property.  This dependency property 
        /// indicates ....
        /// </summary>
        public static void SetVerticalOffset(DependencyObject d, double value)
        {
            d.SetValue(VerticalOffsetProperty, value);
        }

        #endregion
        

        #region EventDate

        /// <summary>
        /// EventDate Attached Dependency Property
        /// </summary>
        /// 
        public static readonly DependencyProperty EventDateProperty =
           DependencyProperty.RegisterAttached("EventDate", typeof(DateTime?),
           typeof(TimelinePanel),
               new PropertyMetadata(new PropertyChangedCallback(OnEventDateChanged)));

        protected static void OnEventDateChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
        }

        /// <summary>
        /// Gets the EventDate property.  This dependency property 
        /// indicates ....
        /// </summary>
        public static DateTime? GetEventDate(DependencyObject d)
        {
            return (DateTime?)d.GetValue(EventDateProperty);
        }

        /// <summary>
        /// Sets the EventDate property.  This dependency property 
        /// indicates ....
        /// </summary>

        public static void SetEventDate(DependencyObject d, DateTime? value)
        {
            d.SetValue(EventDateProperty, value);
        }

        #endregion

        private DateTime _startDate = DateTime.MaxValue;
        private DateTime _endDate = DateTime.MinValue;

        private double ScaleDate(DateTime date)
        {
            TimeSpan totalSpan = _endDate - _startDate;
            TimeSpan myPos = date - _startDate;                       
                        
            return (double)myPos.Ticks / (double)totalSpan.Ticks;
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            double totalWidth = 0;
            double totalHeight = 0;
            
            foreach (UIElement element in Children)
            {
                element.Measure(new Size(double.PositiveInfinity,
                                             double.PositiveInfinity));
                Size childSize = element.DesiredSize;
                totalWidth += childSize.Width;
                totalHeight += childSize.Height;

                DependencyObject child = VisualTreeHelper.GetChild(element, 0);
                if (child != null)
                {
                    DateTime? eventDate = child.GetValue(EventDateProperty) as DateTime?;
                    if (eventDate == null)
                    {
                        continue;
                    }

                    if (eventDate < _startDate)
                    {
                        _startDate = eventDate.Value;
                    }
                    if (eventDate > _endDate)
                    {
                        _endDate = eventDate.Value;
                    }
                }
            }

            return new Size(totalWidth, totalHeight);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            double lastX = 0;
            double lastY = 0;
            foreach (UIElement element in this.Children)
            {
                double pos, left, top;
                DependencyObject child = VisualTreeHelper.GetChild(element, 0);
                if (child != null)
                {
                    DateTime? eventDate = child.GetValue(EventDateProperty) as DateTime?;
                    if (eventDate == null)
                    {
                        continue;
                    }

                    pos = ScaleDate(eventDate.Value);

                    left = finalSize.Width * pos;
                    top = (double)child.GetValue(VerticalOffsetProperty);
                    if (double.IsNaN(top) == true)
                        top = 0.00;
                }
                else
                {
                    continue;
                }

                double width = element.DesiredSize.Width;
                double height = element.DesiredSize.Height;
                if (lastX + width > left)
                {
                    top = lastY + 20 + (double)child.GetValue(VerticalOffsetProperty);
                }
                else
                {
                    top = 0 + (double)child.GetValue(VerticalOffsetProperty);
                }

                element.Arrange(new Rect(left, top, width, height));
                lastX = left;
                lastY = top;                
            }

            return finalSize;
        }
    }

    #endregion
}


